package kbpost

import (
	"context"
	"strings"

	"mall/common/errorx"
	"mall/common/jwtx"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type RecommendPostLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRecommendPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RecommendPostLogic {
	return &RecommendPostLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RecommendPostLogic) RecommendPost(req *types.RecommendPostReq) (resp *types.RecommendPostResp, err error) {
	if len(req.Items) == 0 {
		return nil, errorx.Error(errorx.ERROR_BLOG_POST_ID_REQUIRED, "")
	}

	adminId, err := jwtx.GetUserId(l.ctx)
	if err != nil {
		return nil, errorx.Error(errorx.ERROR_NOT_EXIST_PARAME_X_USER_ID, "")
	}

	var items []string
	for _, item := range req.Items {
		items = append(items, item.Date + ":" + item.PostIds)
	}

	_, err = l.svcCtx.BackendRpc.RecommendPost(l.ctx, &backendclient.RecommendPostReq{
		Items:     strings.Join(items, ";"),
		CreatedBy: adminId,
	})
	if err != nil {
		return nil, err
	}

	return &types.RecommendPostResp{
		Status:  "ok",
		Message: "",
	}, nil
}
